package com.aptech.dbaccess;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBConnection {
    private static String USER_NAME = "sa";
    private static String PASSWORD = "1234567";
    private static String DBNAME = "test";
    private static String SERVER_PORT = "LAB120\\SQL2005";
    private static String URL ="jdbc:sqlserver://" + SERVER_PORT + ";databaseName=" + DBNAME+ ";";

    public static Connection getDBConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        Connection con = null;
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
        return con;
    }

    public static void test() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        Connection con= getDBConnection();
        //INSERT INTO [test].[dbo].[user]([name],[address]) VALUES ('ABC', '123')
        String sql = "SELECT * FROM [USER]";
        PreparedStatement ps = con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getString("username"));
        }
    }

    public static void main(String[] a) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        test();
        int i=0;
        System.out.println(i++);
    }

    public static String getDBNAME() {
        return DBNAME;
    }

    public static void setDBNAME(String aDBNAME) {
        DBNAME = aDBNAME;
    }
}
